package com.hiwan.unions.activity.loginRegister;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.activity.main.MainActivity;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.AESKit;
import com.hiwan.unions.util.Base64Util;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.EditUtils;
import com.hiwan.unions.util.ExampleUtil;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.SystemUtil;
import com.hiwan.unions.util.ToastUtils;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONObject;


/**
 * A login screen that offers login via email/password.
 */
public class LoginUserActivity extends RootActivity implements HttpListener<JSONObject> {
    private EditText edit_phone;
    private EditText edit_password;
    private ImageView imageView;
    String mMobile;
    String password;
    String p = "";
    boolean eyeOpen = false;

    public static void startPreview(Context context) {
        Intent intent = new Intent(context, LoginUserActivity.class);
        context.startActivity(intent);
    }

    public static void startPreview2(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        Intent intent2 = new Intent(context, LoginUserActivity.class);
        Intent[] intents = {intent, intent2};
        context.startActivities(intents);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {

    }

    public void ButtonOnClick(View view) {
        switch (view.getId()) {
            case R.id.wangji:
                RegisterPasswordUpdateActivity.startPreview(this, true);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        setStatusBarColor(R.color.apptheme, true);
        edit_phone = findView(R.id.edit_phone);
        EditUtils.phoneNumAddSpace(edit_phone);
        edit_password = findView(R.id.edit_password);
        setOperationBtn("忘记密码");
        setTitleTxt("登录");
        imageView = findView(R.id.image);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                eyeOpen = EditUtils.YjImage(imageView, edit_password, eyeOpen);
            }
        });
    }


    @Override
    public void getOperationBtn() {
        super.getOperationBtn();
        RegisterPasswordUpdateActivity.startPreview(this, false);
    }

    @Override
    protected void IntentConfirm() {
        super.IntentConfirm();
        try {
            mMobile = CheckUtils.checkMobile(edit_phone);
            password = CheckUtils.checkLoginPaaword(edit_password);
        } catch (ApiException e) {
            ToastUtils.showToast(this, e.getMessage());
            return;
        }
        try {
            JSONObject m = new JSONObject();
            m.put("password", Base64Util.decodeStrNoTry(password));
            m.put("loginname", mMobile);
            m.put("imei", SystemUtil.getImei(this));
            m.put("t", String.valueOf(System.currentTimeMillis()));
            m.put("client", "app");
            m.put("device", "10");
            m.put("phoneType", ExampleUtil.getModel());
            m.put("phoneVersion", ExampleUtil.getRelease());
            m.put("appVersion", ExampleUtil.GetVersion(this));
            m.put("city", userInfoC.getString(ConstantsValues.CITYNAME, "成都市"));
            p = m.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        p = Base64Util.encode(Base64Util.encrypt(p.getBytes(), ConstantsValues.DESPASSWORD.getBytes()));
        Request<JSONObject> request = NoHttp.createJsonObjectRequest(ConstsNet.login, RequestMethod.POST);
        request.add("p", p);
        CallServer.getRequestInstance().add(this, 0, request, this, true);
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        JSONObject jsonObject = response.get();
        jsonObject = JsonUtil.getJSONObject(jsonObject, "data");

        SharedPreferences.Editor editor = userInfo.edit();
        editor.putBoolean(ConstantsValues.IS_HASLOGIN, true);
        editor.putString(ConstantsValues.uuid, AESKit.encryptAndBase64(JsonUtil.getString(jsonObject, "uuid"), ConstantsValues.ASEKIPASSWORD));
        editor.putString(ConstantsValues.AUTHTOKEN, AESKit.encryptAndBase64(JsonUtil.getString(jsonObject, "authtoken"), ConstantsValues.ASEKIPASSWORD));
        editor.putString(ConstantsValues.phone, mMobile);
        editor.commit();
        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {

    }
}

